﻿#pragma once

#ifndef ARD_EVENTBUS_H
#define ARD_EVENTBUS_H

#include <map>
#include <string>
#include <functional>
#include <any>
#include <utils/Enum.h>

class IEvent
{
public:
    IEvent() = default;
    IEvent(const std::string& name, int pri = 127, SyncType type = SyncType::Async);
    IEvent& operator=(const IEvent& other);
    bool operator ==(const IEvent& other)const;
    bool operator < (const IEvent& other)const;

private:
    std::string _name;
    int _priority;
    SyncType _type;
};

struct IEventParam
{
    std::any _params;
};

typedef std::function<void(IEventParam)> IEventFunc;
typedef std::multimap<IEvent, IEventFunc> IEventMap;
class EventBus
{
private:
    static EventBus* singleton;
    EventBus() = default;
    // 不希望线程池对象进行拷贝构造和赋值
    EventBus(const EventBus& s) = default;
    EventBus& operator=(const EventBus& s) = default;
    ~EventBus() = default;
public:
    static EventBus* getInstance();

    //订阅消息
    //void test1(const IEventParam& param) ->  IEventFunc f = test1;
    //EventBus::getInstance()->subscribe(b1,f);
    void subscribe(const IEvent& e, IEventFunc& func);
    //取消订阅消息
    //EventBus::getInstance()->unSubscribe(b1);
    void unSubscribe(const IEvent& e);
    //发布消息
    //EventBus::getInstance()->notify(b1, {._params = 1});
    void notify(const IEvent& e, const IEventParam& param);
private:
    IEventMap _event;
};


#endif // !ARD_EVENTBUS_H
